package de.hska.admin.ui;

import static de.hska.util.LogUtil.begin;
import static de.hska.util.LogUtil.end;
import static de.hska.util.WebConstants.AUTHENTICATION_UI_BEAN;
import static de.hska.util.WebConstants.FWD_START;
import static de.hska.util.EjbConstants.ROLLE_ADMIN;
import static de.hska.util.EjbConstants.ROLLE_KUNDE;
import static de.hska.util.EjbConstants.ROLLE_MITARBEITER;

import java.util.Map;


import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


final public class AuthenticationUiBean {
	private static final Log LOG = LogFactory.getLog(AuthenticationUiBean.class);
	private static final boolean DEBUG = LOG.isDebugEnabled();
	
	private String userName = null;
	private boolean admin = false;
	private boolean mitarbeiter = false;
	private boolean kunde = false;

	/**
	 */
	public static AuthenticationUiBean getInstance() {
		final FacesContext fc = FacesContext.getCurrentInstance();
		final ExternalContext ec = fc.getExternalContext();
		final Map<String, Object> sessionMap = ec.getSessionMap();
		
		AuthenticationUiBean auth = (AuthenticationUiBean) sessionMap.get(AUTHENTICATION_UI_BEAN);
		if (auth == null) {
			auth = new AuthenticationUiBean();
			sessionMap.put(AUTHENTICATION_UI_BEAN, auth);
		}
		return auth;
	}
	
	@Override
	public String toString() {
		return "{userName=" + userName + ", admin=" + admin +
		       ", mitarbeiter=" + mitarbeiter + ", kunde=" + kunde + "}";
	}

	/**
	 * Login von anderen Managed Beans aus. Deshalb static, damit die
	 * anderen Managed Beans nicht das konkrete AuthenticationUiBean suchen m&uuml;ssen.
	 */
	public static String login() {
		if (DEBUG) begin(LOG);
		
		final FacesContext fc = FacesContext.getCurrentInstance();
		final ExternalContext ec = fc.getExternalContext();
		final HttpServletRequest request = (HttpServletRequest) ec.getRequest();
		
		final String remoteUser = request.getRemoteUser();
		if (remoteUser == null) {
			if (DEBUG) end(LOG);
			return null;
		}
		
		final AuthenticationUiBean auth = getInstance();
		auth.userName = remoteUser;
		
		if (request.isUserInRole(ROLLE_ADMIN))
			auth.admin = true;
		if (request.isUserInRole(ROLLE_MITARBEITER))
			auth.mitarbeiter = true;
		if (request.isUserInRole(ROLLE_KUNDE))
			auth.kunde = true;
		
		if (DEBUG) end(LOG, auth);
		return remoteUser;
	}
	
	/**
	 */
	public String logout() {
		if (DEBUG) begin(LOG);

		final FacesContext context = FacesContext.getCurrentInstance();
		final HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
		session.invalidate();

		if (DEBUG) end(LOG);

		// Weiterleitung an Startseite
		return FWD_START;
	}
	

	/*
	 * Keine set-Methoden notwendig, weil die Properties in login() gesetzt werden
	 */
	
	public String getUserName() {
		return userName;
	}
	public boolean isAdmin() {
		return admin;
	}
	public boolean isKunde() {
		return kunde;
	}
	public boolean isMitarbeiter() {
		return mitarbeiter;
	}
}